package com.itheima.reggie.controller;

        import com.baomidou.mybatisplus.core.conditions.Wrapper;
        import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
        import com.baomidou.mybatisplus.core.metadata.IPage;
        import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
        import com.itheima.reggie.common.R;
        import com.itheima.reggie.entity.Category;
        import com.itheima.reggie.service.CategoryService;
        import lombok.extern.slf4j.Slf4j;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;

        import java.util.List;

/**
 * @Description:test
 * @Create by: CAt
 * @Date;2021/9/2621:11
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //添加菜品类(川菜,湘菜)
    @PostMapping
    public R tianjiacai(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }

    //菜品管理
    @GetMapping("/page")
    public R caipingguanli(Integer page,Integer pageSize){
        IPage<Category> pageIof= new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageIof,lambdaQueryWrapper);
        return R.success(pageIof);
    }
    //修改菜品
    @PutMapping
    public R xiugaicai(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }
    //删除菜品
    @DeleteMapping
    public R shanchu(Long id){
        categoryService.panduanshanchu(id);
       // categoryService.removeById(id);
        return R.success("删除成功");
    }
    /*//菜品分类
    @GetMapping("/list")
    public R caipingfenlei(Integer type){
        return null;
    }*/
    @GetMapping("/list")
    public  R  weidao(Category category){
        LambdaQueryWrapper<Category> querWrapper=new LambdaQueryWrapper();
        querWrapper.eq(category!=null,Category::getType,category.getType());
        querWrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(querWrapper);
        return R.success(list);
    }
}
